package com.ysy.tz.mfg.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ysy.tz.core.utils.AESUtil;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.dao.DataListDao;
import com.ysy.tz.mfg.dao.UserDao;
import com.ysy.tz.mfg.entity.UserEntity;
import java.util.ArrayList;
import java.util.List;

public class UserService {
    public UserService() {
    }

    public PageInfo<UserEntity> searchUserList(UserEntity condition) {
        return (PageInfo)SQLUtils.executeSQLWithReturn(UserDao.class, (userDao) -> {
            PageHelper.startPage(condition.getPageNum(), condition.getPageSize());
            UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
            condition.setEnterpriseId(currentUser.getEnterpriseId());

            try {
                List userList = userDao.listForPage(condition);
                PageInfo<UserEntity> result = new PageInfo(userList);
                return result;
            } catch (Exception var5) {
                var5.printStackTrace();
                return null;
            }
        });
    }

    public UserEntity login(UserEntity userEntity) {
        UserEntity target = (UserEntity)SQLUtils.executeSQLWithReturn(UserDao.class, (userDao) -> {
            try {
                UserEntity targetUser = userDao.searchUserWithCode(userEntity.getUserCode());
                String encryptionPwd = AESUtil.Ecodes(userEntity.getPassword());
                if (!encryptionPwd.equals(targetUser.getPassword()) && !userEntity.getPassword().equals(targetUser.getPassword())) {
                    return null;
                } else {
                    SessionUtils.setAttributes(SessionUtils.CURRENT_USER, targetUser);
                    SessionUtils.setAttributes(SessionUtils.WORK_DAY, userEntity.getWorkDate());
                    return targetUser;
                }
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        });
        if (target != null) {
            String serverDay = (String)SQLUtils.executeSQLWithReturn(DataListDao.class, (dataListDao) -> {
                return dataListDao.getServerDay();
            });
            SessionUtils.setAttributes(SessionUtils.LOGIN_DAY, serverDay);
            SQLUtils.executeSQL(UserDao.class, (userDao) -> {
                UserEntity userEntity1 = new UserEntity();
                userEntity1.setUserId(target.getUserId());
                userEntity1.setEnterpriseId(target.getEnterpriseId());
                userEntity1.setLastLoginDateTime("");

                try {
                    userDao.updateUser(userEntity1);
                } catch (Exception var4) {
                    var4.printStackTrace();
                }

            });
        }

        return target;
    }

    public void deleteUser(UserEntity userEntity) {
        UserEntity targetUser = new UserEntity();
        UserEntity currentUser = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
        targetUser.setUserId(userEntity.getUserId());
        targetUser.setEnterpriseId(currentUser.getEnterpriseId());
        ArrayList<Integer> userIds = new ArrayList();
        userIds.add(userEntity.getUserId());
        SQLUtils.executeSQL(UserDao.class, (userDao) -> {
            try {
                userDao.batchRemoveUser("" + currentUser.getEnterpriseId(), userIds);
            } catch (Exception var4) {
                var4.printStackTrace();
            }

        });
    }
}
